package com.admin.socketmultiplayer;

import android.util.Log;
import android.util.Xml;

import com.admin.socketmultiplayer.bean.TestQuestions;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 吴昶 on 2018/3/30.
 */

public class Pullxml {

    public static List<TestQuestions> getPersons(InputStream inStream) throws Exception {
        TestQuestions person = null;
        List<TestQuestions> persons = null;
        XmlPullParser pullParser = Xml.newPullParser();
        pullParser.setInput(inStream, "UTF-8");
        int event = pullParser.getEventType();// 第一個事件
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
                case XmlPullParser.START_DOCUMENT:
                    persons = new ArrayList<TestQuestions>();
                    break;
                case XmlPullParser.START_TAG:
                    if ("problem".equals(pullParser.getName())) {
                        person = new TestQuestions();
                        person.setProblem(pullParser.nextText());
                    }
                    if (person != null) {
                        if ("q1".equals(pullParser.getName())) {
                            person.setQ1(pullParser.nextText());
                        }else if ("q2".equals(pullParser.getName())) {
                                person.setQ2(pullParser.nextText());
                        }else if ("q3".equals(pullParser.getName())) {
                            person.setQ3(pullParser.nextText());
                        }else if ("q4".equals(pullParser.getName())) {
                            person.setQ4(pullParser.nextText());
                        }else if ("choices".equals(pullParser.getName())) {
                            person.setChoices(pullParser.nextText());
                        }else if ("answer".equals(pullParser.getName())) {
                            person.setAnswer(pullParser.nextText());
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("question".equals(pullParser.getName())) {
                        persons.add(person);
                        person = null;
                    }
                    break;
            }
            event = pullParser.next();
        }
        Log.d("=========", "getPersons: "+persons.size());
        return persons;
    }
}
